import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Admin
 * Date: 2022-06-06
 * Time: 20:37
 */
public class TestDemo {
    public static void countSort(int[] array) {
            int max = array[0];
            int min = array[0];
        for (int i = 0; i < array.length; i++) {
            if(max < array[i]) {
                max = array[i];
            }

            if(min > array[i]) {
                min = array[i];
            }
        }

        int len = max - min + 1;
        int[] ret = new int[len];

        for (int i = 0; i < array.length; i++) {
            ret[array[i]-min]++;
        }

        int index = 0;
        for (int i = 0; i < len; i++) {

            while(ret[i] > 0) {
                array[index++] = i + min;
                ret[i]--;
            }
        }

    }

    public static void main(String[] args) {
        int[] array = {8,4,3,1,15,18,22,11,8,2,1,2,3};
        countSort(array);
        System.out.println(Arrays.toString(array));
    }
}
